﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using web.data.entities;
using System.Configuration;
using Webdiyer.WebControls.Mvc;
using System.Web.UI;
using System.Linq.Expressions;
using System.Reflection;
using System.Xml.Schema;
using web.component;

namespace web.bussines
{
    public class partner_service : base_service
    {
        //get all get all
        public PagedList<partners> adm_partner_plist(int pageindex, int pagesize, int language_id)
        {
            using (var data = new baseEntities())
            {
                var list = (from dt in data.tbl_partners
                            where dt.language_id == language_id 
                            select dt);
                list = list.OrderBy(p => p.order_display);
                return list.ToPagedList(pageindex, pagesize);
            }
        }

        //get list partner for home
        public List<partners> partner_list(int language_id)
        {
            using (var data = new baseEntities())
            {
                var list = (from dt in data.tbl_partners
                            where dt.language_id == language_id
                            select dt);
                list = list.OrderBy(p => p.order_display);
                return list.ToList();
            }
        }

        public int adm_partner_update(partners it)
        {
            using (var data = new baseEntities())
            {
                var item = data.tbl_partners.Where(p => p.partner_id == it.partner_id).FirstOrDefault();
                if (item != null)
                {
                    item.partner_id = it.partner_id;
                    item.partner_name = it.partner_name;
                    item.short_information = it.short_information;
                    item.more_information = it.more_information;
                    item.image_path1 = it.image_path1;
                    item.image_path2 = it.image_path2;
                    item.link = it.link;
                    item.date_modified = Udate_time.GetCurrentDateTime();
                    item.language_id = it.language_id;
                    item.order_display = it.order_display;
                    item.keyword = it.keyword;
                    item.description = it.description;
                    item.is_vip = it.is_vip; 

                    data.SaveChanges();
                    return item.partner_id;
                }
                else
                {
                    data.AddTotbl_partners(it);
                    data.SaveChanges();
                    return it.partner_id;
                }
            }
        }
        //enable - disable  
        public int updateStatus(int id, bool is_vip)
        {
            var rt = 0;
            using (var data = new baseEntities())
            {
                var edit_item = data.tbl_partners.Where(p => p.partner_id == id).FirstOrDefault();

                if (edit_item != null) {
                    edit_item.is_vip = is_vip;
                    rt = adm_partner_update(edit_item);
                }
            } return rt;
        }

        //get items by id or code
        public partners getByID(int id)
        {
            using (var data = new baseEntities())
            {
                var item = data.tbl_partners.Where(p => p.partner_id == id).FirstOrDefault();
               // if (item == null)
                //    item = new partners();
                return item;
            }
        }

        //delete by id
        public void deleteItem(int id)
        {
            using (var data = new baseEntities())
            {
                var em = data.tbl_partners.Where(p => p.partner_id == id).FirstOrDefault();

                if (em != null)
                {
                    data.DeleteObject(em);
                    data.SaveChanges();
                }
            }
        }

    }
}
